package com.chezai.client.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chezai.client.R;

import butterknife.BindView;

/**
 * 提示框
 * Created by likunyang on 2017/8/5.
 */

public class NormalAlertDialog extends BaseDialog implements View.OnClickListener {
    public static final int NORMAL_ALERT_DIALOG_CLOSE = 1;
    public static final int NORMAL_ALERT_DIALOG_RIGHT = 2;
    public static final int NORMAL_ALERT_DIALOG_LEFT = 3;
    public static final int NORMAL_ALERT_DIALOG_MIDDLE = 4;

    @BindView(R.id.tvDialogTitle)
    TextView tvDialogTitle;
    @BindView(R.id.ivDialogDelete)
    ImageView ivDialogDelete;
    @BindView(R.id.tvDialogContent)
    TextView tvDialogContent;
    @BindView(R.id.btn_left)
    Button btnLeft;
    @BindView(R.id.btn_right)
    Button btnRight;
    @BindView(R.id.btn_middle)
    Button btnMiddle;
    @BindView(R.id.rl_bottom_double_btn)
    LinearLayout llBottomDoubleBtn;

    @BindView(R.id.flDialog)
    FrameLayout flDialog;

    private OnNormalAlertDialogListener listener;
    private int mTag = -1;

    NormalAlertDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    protected int getDialogLayoutResId() {
        return R.layout.dailog_normal;
    }

    @Override
    protected void initView(Context context) {
    }

    @Override
    protected float getwScale() {
        return 0.7f;
    }

    @Override
    protected float gethScale() {
        return 0.7f;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected void setListener() {
        ivDialogDelete.setOnClickListener(this);
        btnRight.setOnClickListener(this);
        btnMiddle.setOnClickListener(this);
        btnLeft.setOnClickListener(this);
    }

    public int getTag() {
        return mTag;
    }

    public void setTag(int tag) {
        this.mTag = tag;
    }

    private void setOnDialogClickListener(OnNormalAlertDialogListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        int type = -1;
        switch (v.getId()) {
            case R.id.ivDialogDelete:
                type = NORMAL_ALERT_DIALOG_CLOSE;
                break;
            case R.id.btn_right:
                type = NORMAL_ALERT_DIALOG_RIGHT;
                break;
            case R.id.btn_left:
                type = NORMAL_ALERT_DIALOG_LEFT;
                break;
            case R.id.btn_middle:
                type = NORMAL_ALERT_DIALOG_MIDDLE;
                break;
        }
        if (listener != null) {
            listener.onNormalAlertDialogClick(this, v, type);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode && isShowing()) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface OnNormalAlertDialogListener {
        void onNormalAlertDialogClick(NormalAlertDialog dialog, View view, int btnType);
    }


    public static class Builder {
        public static int DIALOG_STYLE_NORMAL = 1;
        public static int DIALOG_STYLE_RED = 2;
        Context context;
        private String title;
        private String content;
        private boolean cancelable;
        private boolean cancelableOutSide;
        private OnNormalAlertDialogListener listener;
        private int titleRes;
        private int contentRes;
        private String btnRightText;
        private String btnMiddleText;
        private String btnLeftText;
        private int titleBackground;
        private int btnSureBackground;
        private int dialogStyle;
        private boolean isSingleMode;
        private boolean rightButtonEnable = true;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder setDialogStyle(int dialogStyle) {
            this.dialogStyle = dialogStyle;
            return this;
        }

        public Builder setSingleBtnMode(boolean isSingleMode) {
            this.isSingleMode = isSingleMode;
            return this;
        }

        public Builder setTitleBackground(int res) {
            this.titleBackground = res;
            return this;
        }

        public Builder setButtonDrawable(int res) {
            this.btnSureBackground = res;
            return this;
        }

        public Builder setRightButtonText(String btnText) {
            this.btnRightText = btnText;
            return this;
        }

        public Builder setLeftButtonText(String btnText) {
            this.btnLeftText = btnText;
            return this;
        }

        public Builder setMiddleButtonText(String btnText) {
            this.btnMiddleText = btnText;
            return this;
        }

        public Builder setTitle(@StringRes int titleRes) {
            this.titleRes = titleRes;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(@StringRes int contentRes) {
            this.contentRes = contentRes;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setCancelable(boolean cancel) {
            this.cancelable = cancel;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean cancel) {
            this.cancelableOutSide = cancel;
            return this;
        }

        public Builder setOnNormalAlertClickListener(OnNormalAlertDialogListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder setRightButtonEnable(boolean rightButtonEnable) {
            this.rightButtonEnable = rightButtonEnable;
            return this;
        }

        public NormalAlertDialog builder() {
            NormalAlertDialog dialog = new NormalAlertDialog(context);
            if (!TextUtils.isEmpty(title)) {
                dialog.tvDialogTitle.setText(title);
            }
            if (titleRes != 0) {
                dialog.tvDialogTitle.setText(titleRes);
            }
            if (!TextUtils.isEmpty(content)) {
                dialog.tvDialogContent.setText(content);
            }

            if (isSingleMode) {
                dialog.llBottomDoubleBtn.setVisibility(View.GONE);
                dialog.btnMiddle.setVisibility(View.VISIBLE);
            } else {
                dialog.llBottomDoubleBtn.setVisibility(View.VISIBLE);
                dialog.btnMiddle.setVisibility(View.GONE);
            }
            if (dialogStyle == DIALOG_STYLE_RED) {
                setTitleBackground(R.mipmap.bg_dialog_red_title);
                setButtonDrawable(R.drawable.shape_bg_dailog_button_red);
            }
            if (contentRes != 0) {
                dialog.tvDialogContent.setText(contentRes);
            }
            if (titleBackground != 0) {
                dialog.flDialog.setBackgroundResource(titleBackground);
            }
            if (btnSureBackground != 0) {
                dialog.btnRight.setBackgroundResource(btnSureBackground);
            }
            dialog.btnRight.setEnabled(rightButtonEnable);
            dialog.btnRight.setText(btnRightText);
            dialog.btnLeft.setText(btnLeftText);
            dialog.btnMiddle.setText(btnMiddleText);
            dialog.setCanceledOnTouchOutside(cancelableOutSide);
            dialog.setCancelable(cancelable);
            dialog.setOnDialogClickListener(listener);
            return dialog;
        }

    }
}
